'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:AdministratorAdminYydjCtrl
 * @description
 * # AdministratorAdminYydjCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
    .controller('AdministratorAdminYydjCtrl', function ($scope, DataService, $rootScope, openApi, Util, $location, $routeParams, FormSubmit) {
        $scope.chooseinfo = function () {
            var relModuleObj = {
                name: "AdministratorAdminYydjCtrl",
                selectTab: "select_seal_by_use",
                selectTabContent: "印章字样"
            };
            // 清掉关联的编号
            var params = $scope.param;
            Util.savaParamForReview(params, $routeParams.id);
            Util.savaDataforsearch(relModuleObj);

            $location.path("finance/choosepeople")
        };
        // 底部关联模块类型选框添加
        $scope.sheetObj = {};
        var carObj = {};
        carObj.sheettitle = "文件类型";
        carObj.callback = function (item) {
            $scope.param.fileType = item.selectTab
            $scope.param.fileTypeContent = item.selectTabContent
        }
        $scope.sheetObj.carObj = carObj;
        $scope.showfileType = function () {
            var param = {}
            param.type = "用印文件类型";
            DataService.getData(openApi.getKeyType, param).then(function (data) {
                console.log(data)
                $scope.sheetObj.carObj.tabList = data
                $scope.showSheet("carObj")
            })

        };
        $scope.showSheet = function (type) {
            var sheetObj = $scope.sheetObj[type]
            $scope.sheetObj.type = type
            $scope.sheetObj.tabList = sheetObj.tabList
            $scope.sheetObj.sheettitle = sheetObj.sheettitle
            $scope.sheetObj.ifshowSheet = true
            $rootScope.showMasker = true
        };
        //获取证件信息列表
        $scope.getChoosecertId = function () {
            Util.saveSelectLists($scope.param.detailList);
            Util.savaParamForReview($scope.param, $routeParams.id);
            $location.path("administrator/chooseinfo/jyz");
        };
        // 删除证件信息列表
        $scope.deletedItem = function (index) {
            $scope.param.detailList.splice(index, 1);
            Util.saveSelectLists($scope.param.detailList);
        };
        var getAllpkIds = function () {
            var pkIds = [],
                pkIdsStr;
            $scope.param.detailList.forEach(function (item, index) {
                pkIds.push(item.pkId);
            });
            pkIdsStr = pkIds.join(",");
            return pkIdsStr;
        }
        $scope.submit = function () {
            if (!$scope.param.sealText) {
                Util.Toast("请选择印章字样");
                return;
            }
            if (!$scope.param.fileType) {
                Util.Toast("请选择用印文件类型");
                return;
            }
            if (!$scope.param.useReason) {
                Util.Toast("请填写用印事由");
                return;
            }
            if (!$scope.param.times) {
                Util.Toast("请填写份数");
                return;
            }
            // 获取证件信息列表id
            $scope.param.sealIds = getAllpkIds();
            // 提交
            var urlParam = "?operationId=-2" + "&&processId=" + $routeParams.id + "&&url=" + openApi.useSeal
            var options = {};
            options.urlParam = urlParam;
            var myDest = {};
            options.param = angular.copy($scope.param, myDest);
            var TVtype;
            TVtype = Util.getTVType(); //根据页面的来源来判断是发起的流程还是修改的流程，如果是发起的则不管，如果是修改的则获取数据用来回显
            // 如果是修改页面的话则提交的地址不一样，参数一样
            delete options.param.detailList
            if (TVtype == Util.UPDATE) {
                options.url = openApi.ip + openApi.useSeal
            }
            new FormSubmit(options);
        };

        var init = function () {
            // 获取回显的详细信息
            Util.getDetailData($routeParams.id, function (data) {
                $scope.param = data;
                // 获取选择页面返回的数据并且合并到参数中
                var selectLists;
                selectLists = Util.getSelectLists();
                $scope.param.detailList = selectLists || $scope.param.detailList || [];
            });
            // 显示审批记录判断
            Util.showloglist($routeParams.id, function (data) {
                $scope.logData = data;
            });
        };
        // 初始化
        init();

    });
